/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-25
*/
package com.rzico.order.service;

import com.alibaba.fastjson.JSON;
import com.rzico.account.entity.Payment;
import com.rzico.account.entity.Refunds;
import com.rzico.account.model.Attach;
import com.rzico.account.service.PaymentService;
import com.rzico.account.service.RefundsService;
import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Coupon;
import com.rzico.basics.entity.CouponCode;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Product;
import com.rzico.basics.mapper.CouponCodeMapper;
import com.rzico.basics.mapper.CouponMapper;
import com.rzico.basics.model.CouponCodeVo;
import com.rzico.exception.CustomException;
import com.rzico.order.entity.Order;
import com.rzico.order.entity.VirtualProduct;
import com.rzico.order.entity.VirtualStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 我的卡券业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */

@Service
public class CouponCodeOrderService extends BaseServiceImpl<CouponCode, String> {

    @Autowired
    private CouponCodeMapper couponCodeMapper;

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private RefundsService refundsService;

    @Override
    public BaseMapper<CouponCode, String> getMapper() {
        return couponCodeMapper;
    }

    //付款
    @Transactional(rollbackFor = Exception.class)
    public void payment(Payment payment) {

        if (!payment.getStatus().equals(Payment.STATUS_WAITING)) {
            throw new CustomException("不是待付款状态");
        }

        payment.setStatus(Payment.STATUS_SUCCESS);
        payment.setPaymentDate(new Date());
        paymentService.updateByPrimaryKeySelective(payment);

        Attach attach = payment.decodeAttach();

        Map<String,Object> params = new HashMap<>();
        params.put("memberId",payment.getMemberId());
        params.put("productId",attach.getProductId());

        BigDecimal quantity = attach.getQuantity();

        List<CouponCodeVo> couponCodeVoList = couponCodeMapper.selectExchangeVoList(params);

        for (CouponCodeVo couponCodeVo:couponCodeVoList) {
            if (quantity.compareTo(BigDecimal.ZERO)>0) {
                CouponCode couponCode = couponCodeMapper.selectByPrimaryKey(couponCodeVo.getId());
                couponCode.setIsUsed(true);
                couponCode.setUsedDate(new Date());
                couponCodeMapper.updateByPrimaryKeySelective(couponCode);
                quantity = quantity.subtract(BigDecimal.ONE);
            }
        }

    }

    //退款
    @Transactional(rollbackFor = Exception.class)
    public void refunds(Refunds refunds) {
        if (!refunds.getStatus().equals(Refunds.STATUS_WAITING)) {
            throw new CustomException("不是待退款状态");
        }
        refunds.setStatus(Refunds.STATUS_REFUNDED);
        refunds.setRefundsDate(new Date());
        refundsService.updateByPrimaryKeySelective(refunds);

        Attach attach = refunds.decodeAttach();

        Map<String,Object> params = new HashMap<>();
        params.put("memberId",refunds.getMemberId());
        params.put("productId",attach.getProductId());

        BigDecimal quantity = attach.getQuantity();

        List<CouponCodeVo> couponCodeVoList = couponCodeMapper.selectExchangeUseVoList(params);

        for (CouponCodeVo couponCodeVo:couponCodeVoList) {
            if (quantity.compareTo(BigDecimal.ZERO)>0) {
                CouponCode couponCode = couponCodeMapper.selectByPrimaryKey(couponCodeVo.getId());
                couponCode.setIsUsed(false);
                couponCode.setUsedDate(null);
                couponCodeMapper.updateByPrimaryKeySelective(couponCode);
                quantity = quantity.subtract(BigDecimal.ONE);
            }
        }

//        if (quantity.compareTo(BigDecimal.ZERO)>0) {
//            throw new CustomException("退还兑换券失败");
//        }
//
//        if (refunds.getStatus().equals(Refunds.STATUS_REFUNDED) || refunds.getStatus().equals(Refunds.STATUS_SUCCESS)) {
//            try {
//                //退知退款完成
//                refundsService.handle(refunds.getSn(), null);
//            } catch (Exception e) {
//                throw new CustomException(e.getMessage());
//            }
//        }

    }
}
